﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class TuyenVanChuyenDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {


            TuyenVanChuyenDTO objectDTO = new TuyenVanChuyenDTO();
            objectDTO.MaTuyen = int.Parse(dt.Rows[i]["MaTuyen"].ToString());
            objectDTO.TenTuyen = dt.Rows[i]["TenTuyen"].ToString();
            objectDTO.DiemDau = int.Parse(dt.Rows[i]["DiemDau"].ToString());
            objectDTO.DiemCuoi = int.Parse(dt.Rows[i]["DiemCuoi"].ToString());
            // objectDTO.TongSoKm = float.Parse(dt.Rows[i]["TongSoKm"].ToString());
            // objectDTO.TongThoiGian = dt.Rows[i]["TongThoiGian"].ToString();
            return (object)objectDTO;
        }

        public TuyenVanChuyenDTO[] GetAll()
        {
            String strSQL = "Select * from TuyenVanChuyen ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            TuyenVanChuyenDTO[] tuyenvanchuyenDTOs = new TuyenVanChuyenDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                tuyenvanchuyenDTOs[i] = (TuyenVanChuyenDTO)GetDataFromDataRow(dt, i);

            return tuyenvanchuyenDTOs;
        }

        public bool insert(TuyenVanChuyenDTO info)
        {
            //bool falg = falg;
            string insertCommand = "INSERT TuyenVanChuyen(TenTuyen,DiemDau,DiemCuoi) VALUES(N'" +
                info.TenTuyen + "', '" +
                info.DiemDau + "', '" +
                info.DiemCuoi + "')";
            // info.TongSoKm + "', '" +
            // info.TongThoiGian + "')";

            return ExecuteNonQuery(insertCommand);
        }

        public void update(TuyenVanChuyenDTO info)
        {
            string updateCommand = "UPDATE TuyenVanChuyen " +
                                    "SET TenTuyen = '" + info.TenTuyen + "', " +
                                    " DiemDau = '" + info.DiemDau + "', " +
                                    " DiemCuoi = '" + info.DiemCuoi + "', " +
                                    " TongSoKm = '" + info.TongSoKm + "'," +
                                    " TongThoiGian = '" + info.TongThoiGian + "'" +
                                    " WHERE MaTuyen = " + info.MaTuyen;


            ExecuteNonQuery(updateCommand);
        }

        public void delete(string MaTuyenVanChuyen)
        {
            string deleteCommand = "DELETE FROM TUYENVANCHUYEN WHERE MaTuyenVanChuyen = " + MaTuyenVanChuyen;
            ExecuteNonQuery(deleteCommand);
        }

    }
}
